/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package doancongnghephanmem;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author My PC
 */
public class NhanVien extends javax.swing.JFrame {
    String Manvnhanvien;
    DefaultTableModel model;

    public String getManvnhanvien() {
        return Manvnhanvien;
    }

    public void setManvnhanvien(String Manvnhanvien) {
        this.Manvnhanvien = Manvnhanvien;
        this.loadEmployee();
    }
    /**
     * Creates new form NhanVien
     */
    public NhanVien() {
        initComponents();
        model = (DefaultTableModel) jTable1.getModel();
    }
public void loadEmployee() {
    model = (DefaultTableModel) jTable1.getModel();
        Connection conn = null;
        String connStr = "jdbc:sqlserver://MYPC\\SQLEXPRESS:1433;databaseName=QLNgayNghi;username=sa;password=110691";
        //WINDWALK-PC\SQLEXPRESS
        String strSQL;
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            conn = DriverManager.getConnection(connStr);
            strSQL = "select * from GiamDocDuyet where MaNV = '"+this.Manvnhanvien+"'";
            Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            try (ResultSet rs = stmt.executeQuery(strSQL)) {
                while(model.getRowCount()>0)
        model.removeRow(0);
                while (rs.next()) {
                    Vector v = new Vector();
                    v.add(rs.getString(1));
                    v.add(rs.getString(2));
                    v.add(rs.getString(3));
                    v.add(rs.getDate(4));
                    v.add(rs.getInt(5));
                    v.add(rs.getString(6));
                    v.add(rs.getString(7));
                    v.add(rs.getString(8));
                    model.addRow(v);
                }
                jTable1.setModel(model);
            }
            conn.close();
        } catch (SQLException ex) {
            System.out.println("Wrong with connection string! Check again");
            System.out.println(ex.getMessage());
        } catch (ClassNotFoundException ex) {
            System.out.println("Error loading driver! Check again");
            System.out.println(ex.getMessage());
        }
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        bt_xemthongtin = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jLabel3 = new javax.swing.JLabel();
        bt_taodon = new javax.swing.JButton();
        bt_rutdon = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBackground(new java.awt.Color(153, 255, 255));

        bt_xemthongtin.setFont(new java.awt.Font("Times New Roman", 1, 18)); // NOI18N
        bt_xemthongtin.setForeground(new java.awt.Color(0, 51, 204));
        bt_xemthongtin.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Search.png"))); // NOI18N
        bt_xemthongtin.setText("Xem thông tin ");
        bt_xemthongtin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_xemthongtinActionPerformed(evt);
            }
        });

        jButton4.setFont(new java.awt.Font("Times New Roman", 1, 18)); // NOI18N
        jButton4.setForeground(new java.awt.Color(0, 51, 255));
        jButton4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Back.png"))); // NOI18N
        jButton4.setText("Back");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null}
            },
            new String [] {
                "Họ tên", "Mã NV", "Chức vụ", "Nghỉ từ ngày", "Số ngày nghỉ", "Lý do", "Ghi chú", "Quyết định"
            }
        ));
        jScrollPane2.setViewportView(jTable1);

        jLabel3.setFont(new java.awt.Font("Times New Roman", 3, 18)); // NOI18N
        jLabel3.setForeground(new java.awt.Color(0, 51, 204));
        jLabel3.setText("Bạn đã đăng nhập bằng quyền nhân viên ");

        bt_taodon.setFont(new java.awt.Font("Times New Roman", 1, 18)); // NOI18N
        bt_taodon.setForeground(new java.awt.Color(0, 51, 204));
        bt_taodon.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Add.png"))); // NOI18N
        bt_taodon.setText("Tạo đơn ");
        bt_taodon.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_taodonActionPerformed(evt);
            }
        });

        bt_rutdon.setFont(new java.awt.Font("Times New Roman", 1, 18)); // NOI18N
        bt_rutdon.setForeground(new java.awt.Color(0, 51, 204));
        bt_rutdon.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Delete.png"))); // NOI18N
        bt_rutdon.setText("Rút đơn ");
        bt_rutdon.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_rutdonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                        .addGap(19, 19, 19)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(64, 64, 64)
                                .addComponent(bt_taodon, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(67, 67, 67)
                                .addComponent(bt_rutdon, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(bt_xemthongtin)
                                .addGap(78, 78, 78))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 818, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(0, 0, Short.MAX_VALUE)))))
                .addGap(38, 38, 38))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(38, 38, 38)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(bt_taodon)
                    .addComponent(bt_rutdon)
                    .addComponent(bt_xemthongtin))
                .addGap(32, 32, 32)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void bt_taodonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_taodonActionPerformed
                 try {
            // TODO add your handling code here:
             Connection conn =null;  
        String StringConn = "jdbc:sqlserver://MYPC\\SQLEXPRESS:1433;databaseName=QLNgayNghi;user=sa;password=110691";
        String strSQL = null;
            try {
                // TODO add your handling code here:
                          //buoc 1: load driver
                          Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(NhanVienMoi.class.getName()).log(Level.SEVERE, null, ex);
            }
                    //buoc 2: tao doi tuong ket noi
                    conn = DriverManager.getConnection(StringConn);
                    //buoc 3: viet cau lenh SQL
                    strSQL = "select * from NhanVien where MaNV = '"+this.Manvnhanvien+"'";
                     
                    //buoc 4: tao doi tuong Statement
                    Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
                    ResultSet rs = stmt.executeQuery(strSQL);
                    
                    while (rs.next()) {
                        if (rs.getInt(9) < 0) {
                          JOptionPane.showMessageDialog(null, "hết số ngày nghỉ ");

                        }
                        else{
                         Nhanvien_Taodon nvtd = new Nhanvien_Taodon();
                nvtd.setManvnhanvien(Manvnhanvien);
                nvtd.setVisible(true);
                        }
                    
                    
                    }
                    
                    conn.close();
                    //buoc 5: thuc thi cau lenh sql
                    
                    conn.close();
        } catch (SQLException ex) {
            Logger.getLogger(NhanVienMoi.class.getName()).log(Level.SEVERE, null, ex);
        }
    
               

            
             
    }//GEN-LAST:event_bt_taodonActionPerformed

    private void bt_rutdonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_rutdonActionPerformed
        NhanVien_Rutdon nvtd = new NhanVien_Rutdon();
        nvtd.setManv(Manvnhanvien);
        nvtd.setVisible(true);
         
    }//GEN-LAST:event_bt_rutdonActionPerformed

    private void bt_xemthongtinActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_xemthongtinActionPerformed
        try {
            NhanVien_Xemthongtin nhtt = new NhanVien_Xemthongtin();
            nhtt.setManv(Manvnhanvien);
            nhtt.setVisible(true);
        } catch (SQLException ex) {
            Logger.getLogger(NhanVien.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_bt_xemthongtinActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        this.setVisible(false);
        new chaomung().setVisible(true);
    }//GEN-LAST:event_jButton4ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(NhanVien.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(NhanVien.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(NhanVien.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(NhanVien.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new NhanVien().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bt_rutdon;
    private javax.swing.JButton bt_taodon;
    private javax.swing.JButton bt_xemthongtin;
    private javax.swing.JButton jButton4;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTable1;
    // End of variables declaration//GEN-END:variables

    private Object Nhanvien_Taodon() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
}
